package com.open.basic.exception;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;
import java.util.function.Predicate;


/**
 * @author: system
 * @Date: 2024/9/19 下午4:46
 * @Version: V1.0
 * @description: -> 函数式编程测试
 */
@Slf4j
public class ExFunction {
    private static final Long MIN = 1000000000L;

    private static final Long MAX = 9999999999L;

    private static final AtomicLong ATOMIC_LONG = new AtomicLong(MIN);

    public static void main(String[] args) {
        Function<AtomicLong, Long> function = (ATOMIC_LONG) -> {
            Long andAdd = ATOMIC_LONG.getAndAdd(1);
            if (andAdd > MAX) {
                andAdd = MIN;
                ATOMIC_LONG.set(andAdd);
            }
            return andAdd;
        };
        Long l = get(function, ATOMIC_LONG);
        Long l1 = get(function, ATOMIC_LONG);
        Long l2 = get(function, ATOMIC_LONG);
        System.out.println(l);
        System.out.println(l1);
        System.out.println(l2);
    }
    public static <T, U> U get(Function<T, U> function, T param) {
        try {
            // Function<AtomicLong, Long> function = (ATOMIC_LONG) -> {
            //     Long andAdd = ATOMIC_LONG.getAndAdd(1);
            //     if (andAdd > MAX) {
            //         andAdd = MIN;
            //         ATOMIC_LONG.set(andAdd);
            //     }
            //     return andAdd;
            // };
            U apply = function.apply(param);
            TimeUnit.MILLISECONDS.sleep(50);
            return apply;
        } catch (InterruptedException e) {
            log.error("getId sleep error：", e);
        }
        return null;
    }
}
